Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
I changed the class name in document via "getElementById" but it doesn't take effect

Thank you for advance. I changed the class name in JavaScript, but it doesn't change. [html]

<button id={{ product.id }} data-product={{ product.id }} data-action='add' class="btn btn-outline-secondary add-btn store-cart"> button</button>

[javascript]

var storeBtns = document.getElementsByClassName('store-cart')

for (i = 0; i < storeBtns.length; i++) {
    storeBtns[i].addEventListener('click', function(){
        document.getElementById(this.dataset.product).class = 'btn-primary'})}

When I check (document.getElementById(this.dataset.product).class by adding alert to the bottom line, "btn-primary" appears, but this does not apply in the page. What should I do?

I also tried with classList.add , but it didn't work, so I was wondering if there is anything else to do, such as returning document .

Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

classtList.add('class-name') should be what you need: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

That's assuming you want to append btn-primary to the classes already on the element.

for (i = 0; i < storeBtns.length; i++) {
    storeBtns[i].addEventListener('click', function(){
        document.getElementById(this.dataset.product).classList.add('btn-primary');
   });
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!